<?php

namespace App\Models;

use App\User;

class Product extends BaseModel
{
    //获取产品详情 包含产品规格
    public static function getOne()
    {
        $product = self::select('id','title','description','image','on_sale','sold_count','review_count','price','img_group','vip_price','is_hot','weight')->first();
        if ($product) {
            $product = $product->toArray();
            $product['is_proxy'] = User::isProxy();
            $product['image'] = self::jsonDecodeImg($product['image']);
            $product['img_group'] = self::jsonDecodeImg($product['img_group']);
            $product['stock'] = ProductSku::getStockCount($product['id']);
            $product['spec'] = ProductSpec::getSpec($product['id']);
            $product['stock'] = ProductSku::getStockCount($product['id']);
        }
        return $product;
    }

    //获取产品 不含产品规格
    public static function getProduct($id,$field='')
    {
        $product = self::select('id','title','description','image','on_sale','sold_count','review_count','price','img_group','vip_price','is_hot','weight')->find($id);
        if ($product) {
            $product = $product->toArray();
            $product['image'] = self::jsonDecodeImg($product['image']);
            $product['img_group'] = self::jsonDecodeImg($product['img_group']);
            $product['is_proxy'] = User::isProxy();
            $product['stock'] = ProductSku::getStockCount($product['id']);
        }
        if (array_key_exists($field,$product)) {
            if ($field == 'weight') {
                return (int)$product[$field];
            }
            return $product[$field];
        }
        return $product;
    }

    public static function getPageList($where = [], $orderBy = ['id','desc'])
    {
        $where[] = [ 'on_sale', '=', 1];
        list($order, $sort) = $orderBy;
        $list = self::where($where)
            ->select('id','title','description','image','on_sale','sold_count','review_count','price','price','vip_price','is_hot','weight')
            ->OrderBy($order, $sort)
            ->paginate(10)
            ->toArray();
        foreach($list['data'] as $key => &$val) {
            $val['is_proxy'] = User::isProxy();
            $val['image'] = self::jsonDecodeImg($val['image']);
            $val['stock'] = ProductSku::getStockCount($val['id']);
        }
        return $list;
    }


    public function setImgGroupAttribute($pictures)
    {
        if (is_array($pictures)) {
            $this->attributes['img_group'] = json_encode($pictures);
        }
    }

    public function getImgGroupAttribute($pictures)
    {
        return json_decode($pictures, true);
    }

    public function getImageUrlAttribute()
    {
        // 如果 image 字段本身就已经是完整的 url 就直接返回
        if (Str::startsWith($this->attributes['image'], ['http://', 'https://'])) {
            return $this->attributes['image'];
        }
        return \Storage::disk('public')->url($this->attributes['image']);
    }

    public function getSkuAttribute($sku)
    {
        return unserialize($sku);
    }

    public function getSpecAttribute($spec)
    {
        return unserialize($spec);
    }
}
